package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Idea;
import com.kaishengit.entity.IdeaComment;
import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class IdeaCommentDao {

	private DBHelp<IdeaComment> db=new DBHelp<IdeaComment>();
	
	public void save(IdeaComment ic){
		final String sql="INSERT INTO t_ideacomment(id,content,createtime,t_idea_id,t_user_id)VALUES(?,?,?,?,?) ";
		db.executeSQL(sql, ic.getId(),ic.getContent(),ic.getCreatetime(),ic.getIdeaId(),ic.getUserId());
		
	}
	public void update(String content,String IdeaCommentId){
		
		final String sql="UPDATE t_ideacomment SET content=? WHERE id=? ";
		db.executeSQL(sql, content,IdeaCommentId);
	}
	
	/*
	 * 
	 * SELECT tic.id,tic.content,tic.createtime,tic.t_user_id ,tu.name,tu.pic,ti.title,ti.content AS tcontent,ti.createtime AS tcreatime FROM t_ideacomment AS tic
LEFT JOIN t_user AS tu ON tic.t_user_id=tu.id
LEFT JOIN t_idea AS ti ON tic.t_idea_id=ti.id
WHERE tic.t_idea_id='2';
	 * */
	public List<IdeaComment> findById(String IdeaId){
		StringBuilder sql=new StringBuilder();
		sql.append(" SELECT tic.id,tic.content,tic.createtime,tic.t_user_id ,tu.name,tu.pic,tic.t_idea_id,ti.title,ti.content AS tcontent,ti.createtime AS tcreatetime ");
		sql.append(" FROM t_ideacomment AS tic ");
		sql.append(" LEFT JOIN t_user AS tu ON tic.t_user_id=tu.id ");
		sql.append(" LEFT JOIN t_idea AS ti ON tic.t_idea_id=ti.id ");
		sql.append(" WHERE tic.t_idea_id=? ");
		return db.executeQueryForList(sql.toString(), new IdeaCommentRowMapper(), IdeaId);
	}
	
public IdeaComment findByIdeaCommentId(String IdeaCommentId){
	StringBuilder sql=new StringBuilder();
	sql.append(" SELECT tic.id,tic.content,tic.createtime,tic.t_user_id ,tu.name,tu.pic,tic.t_idea_id,ti.title,ti.content AS tcontent,ti.createtime AS tcreatetime ");
	sql.append(" FROM t_ideacomment AS tic ");
	sql.append(" LEFT JOIN t_user AS tu ON tic.t_user_id=tu.id ");
	sql.append(" LEFT JOIN t_idea AS ti ON tic.t_idea_id=ti.id ");
	sql.append(" WHERE tic.id=? ");
	return db.executeQueryForObject(sql.toString(), new IdeaCommentRowMapper(),IdeaCommentId);
	
}
	
	public void dele(String ideaid){
		String sql="DELETE FROM t_ideacomment WHERE t_idea_id=? ";
		 db.executeSQL(sql, ideaid);
		
	}
	public void Del(String id){
		 String sql="DELETE FROM t_ideacomment WHERE id=? ";
		 db.executeSQL(sql,id);
	}

	public class IdeaCommentRowMapper implements RowMapper<IdeaComment> {

	    	public IdeaComment mapperRow(ResultSet rs) throws SQLException {
			IdeaComment ic=new IdeaComment();
			ic.setId(rs.getString("id"));
			ic.setContent(rs.getString("content"));
			ic.setCreatetime(rs.getString("createtime"));
			
			User user=new User();
			user.setId(rs.getString("t_user_id"));
			user.setName(rs.getString("name"));
			user.setPic(rs.getString("pic"));
	
			ic.setUser(user);
			ic.setUserId(rs.getString("t_user_id"));
			
			Idea idea=new Idea();
			idea.setId(rs.getString("t_idea_id"));
			idea.setTitle(rs.getString("title"));
			idea.setContent(rs.getString("tcontent"));
			idea.setCreatetime(rs.getString("tcreatetime"));
			 
			ic.setIdea(idea);
			ic.setIdeaId(rs.getString ("t_idea_id"));
			
			
			
			return ic;
		}

	}


}
